<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "myDBPDO";

try {

  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  VALUES ('John', 'Doe', 'john@example.com')";

  //使用exec()
  $conn->exec($sql);
  echo "新记录插入成功";
} catch (\Throwable $th) {
  echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
